from rest_framework.filters import BaseFilterBackend

# 最小值
class MinIdBaseFilterBackend(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        # 获取前端传来的最小值 id
        min_id = request.query_params.get("min_id")
        print(f"min_id:{min_id}")
        # 如果最小值id存在
        if min_id:
            return queryset.filter(id__lt=min_id).order_by("-id")
        return queryset


# 最大值
class MaxIdBaseFilterBackend(BaseFilterBackend):

    def filter_queryset(self, request, queryset, view):
        # 获取前端传来的最小值 id
        max_id = request.query_params.get("max_id")
        print(f"max_id:{max_id}")
        # 如果最小值id存在
        if max_id:
            return queryset.filter(id__gt=max_id).order_by("id")
        return queryset

